﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Diagnostics;
namespace Launchr
{
    class CGunZ
    {
        private string authCode;
        private void startProcess(string procName, string args)
        {
            try
            {
                Process.Start(procName, args);
            }
            catch (Exception)
            {
                Exception errorProcess = new Exception("-1|There was an error launching " + procName + ". Does the file exist?");
                throw errorProcess;
            }
        }
        private string getLaunchString( string request )
        {
            try
            {
                TcpClient ijjiConnection = new TcpClient( "gstr.ijjimax.com", 10000 );
                StreamReader readFromConnection = new StreamReader( ijjiConnection.GetStream( ) );
                StreamWriter writeToConnection = new StreamWriter( ijjiConnection.GetStream( ) );
                string httpRequest = "GET /?";
                httpRequest += request;
                httpRequest += " HTTP/1.0\r\n";
                httpRequest += "User-Agent: Microsoft Internet Explorer\r\n";
                httpRequest += "Pragma: no-cache\r\n";
                httpRequest += "\r\n";
                writeToConnection.WriteLine( httpRequest );
                writeToConnection.Flush( );

                string launchkey = readFromConnection.ReadToEnd( );

                ijjiConnection.Close( );//Close connections.
                readFromConnection.Close( );
                writeToConnection.Close( );
                return launchkey;
            }
            catch ( Exception ex )
            {
                throw ex;
            }
        }

        public void startGunz( )
        {
            string launchString = string.Empty;
            try
            {
                launchString = getLaunchString( authCode.Split( '?' )[ 1 ] );
            }
            catch ( Exception ex )
            {
                throw ex;
            }
            startProcess( "C:\\IJJI\\ENGLISH\\Gunz\\Gunz.exe", "-key " + launchString );
        }
        public bool authenticateUser(string username, string password)
        {
            HttpWebRequest ijjiRequest      = (HttpWebRequest)HttpWebRequest.Create("http://auth.ijji.com/auth.nhn?memberid=" + username + "&password=" + password);
            CookieContainer loginCookies    = new CookieContainer();
            ijjiRequest.CookieContainer     = loginCookies;
            HttpWebResponse ijjiResponse    = (HttpWebResponse)ijjiRequest.GetResponse();
            StreamReader readResponse       = new StreamReader(ijjiResponse.GetResponseStream());
            string serverResponse           = readResponse.ReadToEnd();
            if (serverResponse.StartsWith("1"))
            {
                ijjiRequest = (HttpWebRequest)HttpWebRequest.Create("http://gunz.ijji.com/common/launch.nhn?gameId=u_gunz");
                ijjiRequest.CookieContainer = loginCookies;
                ijjiResponse = (HttpWebResponse)ijjiRequest.GetResponse();
                readResponse = new StreamReader(ijjiResponse.GetResponseStream());
                authCode = readResponse.ReadToEnd();
                if (authCode.Contains("\"isLogin\":true"))
                {
                    return true;
                }
                return false;
            }
            if (serverResponse.StartsWith("-1"))
            {
                Exception memberMissing = new Exception("The supplied member ID does not exist");
                throw memberMissing;
            }
            if (serverResponse.StartsWith("-2"))
            {
                Exception wrongPassword = new Exception("The supplied password is incorrect");
                throw wrongPassword;
            }
            if (serverResponse.StartsWith("-3"))
            {
                Exception accountTerminated = new Exception("The account has been terminated");
                throw accountTerminated;
            }
            if (serverResponse.StartsWith("-4"))
            {
                Exception accountBanned = new Exception("The account is banned");
                throw accountBanned;
            }
            return false;
        }
    }
}
